package com.wq.spit.controller;

import com.wq.common.entity.PageResult;
import com.wq.common.entity.Result;
import com.wq.common.entity.StatusCode;
import com.wq.spit.dao.SpitDao;
import com.wq.spit.entity.Spit;
import com.wq.spit.service.SpitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("spit")
public class SpitCtroller {
    @Autowired
    private SpitService spitService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SpitDao spitDao;

    @GetMapping
    public Result findAll() {
        List<Spit> all = spitService.findAll();
        return new Result(true, StatusCode.OK, "查询所有成功", all);
    }

    @GetMapping("{id}")
    public Result findById(@PathVariable String id) {
        Spit spit = spitService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", spit);
    }

    @GetMapping("page/{parentid}")
    public Result findByParentId(String parentid, @RequestParam(value = "page", defaultValue = "2") int page, @RequestParam(value = "size", defaultValue = "2") int size) {
        Page page1 = spitService.findByParentid(parentid, page, size);
        return new Result(true, StatusCode.OK, "分页查询成功", new PageResult<Spit>(page1.getTotalElements(), page1.getContent()));
    }

    @PutMapping("{id}")
    public Result thumbup(@PathVariable String id) {
        //模拟用户1013
        String userid = "1013";
        Object obj = redisTemplate.boundValueOps("thumbup_" + userid + "_" + id).get();
        if (StringUtils.isEmpty(obj)) {
//            查询redis数据库为null，表示用户对当前id的文章没有点赞过
            spitService.Thumbup(id);
            redisTemplate.boundValueOps("thumbup_" + userid + "_" + id).set(1);
            return new Result(true, StatusCode.OK, "点赞成功");
        } else {
            Spit spit = spitDao.findById(id).get();
            spit.setThumbup(spit.getThumbup() -1);
            spitDao.save(spit);
            return new Result(false, StatusCode.REPERROR, " 取消点赞");
        }
    }
    @DeleteMapping("{id}")
    public Result Delect(@PathVariable String id){
        spitService.delectById(id);
        return new Result(false, StatusCode.REPERROR, "删除成功");
    }
    @PostMapping
    public Result save(@RequestBody Spit spit) {
        spitService.insert(spit);
        return new Result(true, StatusCode.OK, "新增成功");
    }
    @PutMapping
    public Result update(Spit spit){
        spitService.update(spit);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    @PutMapping("visits/{id}")
    public Result visits(@PathVariable String id) {
        //模拟用户1013
        String userid = "1013";

            Object obj = redisTemplate.boundValueOps("visits" + userid + "_" + id).get();
            spitService.visits(id);
            redisTemplate.boundValueOps("visits" + userid + "_" + id).set(1);
            return new Result(true, StatusCode.OK, "吐槽成功");

    }
}
